package com.feicheng.lanniao.controller.pc;

import java.time.LocalDateTime;

import com.feicheng.lanniao.anno.StaticSelectCountOfCarSellInfo;
import com.feicheng.lanniao.exception.SystemException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.feicheng.lanniao.entity.Car;
import com.feicheng.lanniao.entity.CarRentalInfo;
import com.feicheng.lanniao.entity.CarRentalOwner;
import com.feicheng.lanniao.entity.CarSellClientVo;
import com.feicheng.lanniao.entity.CarSellInfo;
import com.feicheng.lanniao.entity.CarSellOwner;
import com.feicheng.lanniao.result.ResultInfo;
import com.feicheng.lanniao.service.ICarSellClientVoService;
import com.feicheng.lanniao.service.ICarSellInfoService;
import com.feicheng.lanniao.service.ICarSellOwnerService;
import com.feicheng.lanniao.service.ICarService;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author feichengTech
 * @since 2021-09-02
 */
@RestController
@RequestMapping("/lanniao-user/car-sell-info")
public class CarSellInfoClientPcController {
	@Autowired
	ICarSellInfoService iCarSellInfoService;
	@Autowired
	ICarSellClientVoService iCarSellClientVoService;
	@Autowired
	ICarService iCarService;
	@Autowired
	ICarSellOwnerService iCarSellOwnerService;
	@GetMapping("/page")
	public ResultInfo carSellList(
			String searchKey,Boolean dec,String order,Integer clientId , Integer conditionId, Integer mileageId, Integer brandId,
			Integer sellPriceId, Integer carSellPriceFrom, Integer carSellPriceTo,String type,String ownerShip,
			@RequestParam(required = false,defaultValue = "1") Integer currentPage,
			@RequestParam(required = false,defaultValue = "10") Integer pageSize) {
		return iCarSellClientVoService.getCarSellClientVoPage(searchKey,dec ,order,clientId, conditionId, mileageId, brandId, sellPriceId,carSellPriceFrom,carSellPriceTo,type, ownerShip, currentPage, pageSize);
	}

	//@StaticSelectCountOfCarSellInfo
	@GetMapping("/info")
	public ResultInfo oneCarSellClientVo(Integer carSellInfoId) {
		return iCarSellClientVoService.getCarSellClientVoById(carSellInfoId);
	}

	@DeleteMapping("/info")
	public ResultInfo deleteCarSellClientInfo(Integer carSellInfoId) {
		return iCarSellInfoService.deleteCarSellInfoById(carSellInfoId);
	}

	@PutMapping("/info")
	public ResultInfo updateCarSellInfo(CarSellClientVo carSellClientVo) {
		System.out.println("--------------------" + carSellClientVo);
		return iCarSellInfoService.updateCarSellInfo(carSellClientVo);
	}

	@PostMapping("/info")
	public ResultInfo addCarSellInfo(
			Integer carCondition, // not null 车龄
			Integer mileage,   //not null   里程
			String ownerShip,
			Integer brandId,   //not null
			String type,       // 车型 not null
			String imgUrl,
			Integer sellPrice,
			Integer clientId,   // not null
			String tel,
			String wxNumber
			) {
		Car car = new Car();
		car.setCarCondition(carCondition);
		car.setMileage(mileage);
		car.setOwnerShip(ownerShip);
		car.setBrandId(brandId);
		car.setType(type);
		car.setImgUrl(imgUrl);
		car.setSellPrice(sellPrice);
		iCarService.save(car);

		//创建CarSellInfo对象
		CarSellInfo carSellInfo = new CarSellInfo();
		carSellInfo.setCarId(car.getCarId());
		carSellInfo.setClientId(clientId);
		carSellInfo.setSellPrice(car.getSellPrice());
		carSellInfo.setTel(tel);
		carSellInfo.setWxNumber(wxNumber);
		iCarSellInfoService.save(carSellInfo);
		//创建中间表对象
		CarSellOwner carSellOwner = new CarSellOwner();
		if(clientId == null){
			throw new SystemException("clientId不能为空");
		}
		carSellOwner.setClientId(clientId);
		carSellOwner.setCarSellInfoId(carSellInfo.getCarSellInfoId());
		iCarSellOwnerService.save(carSellOwner);
		return ResultInfo.success();
	}
}
